package com.itheima.crm.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itheima.common.utils.Page;
import com.itheima.crm.pojo.BaseDict;
import com.itheima.crm.pojo.Customer;
import com.itheima.crm.pojo.QueryVo;
import com.itheima.crm.service.BaseDictService;
import com.itheima.crm.service.CustomerService;

@Controller
@RequestMapping("customer")
public class CustomerController {
	
	@Autowired
	private BaseDictService baseDictService;
	@Autowired
	private CustomerService customerService;
	@Value("${CUSTOMER_FROM_TYPE}")
	private String CUSTOMER_FROM_TYPE;
	@Value("${CUSTOMER_INDUSTRY_TYPE}")
	private String CUSTOMER_INDUSTRY_TYPE;
	@Value("${CUSTOMER_LEVEL_TYPE}")
	private String CUSTOMER_LEVEL_TYPE;
	
	/**
	 * 显示用户列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "list.action")
	public String queryCustomerList(QueryVo vo, Model model) {
		//客户来源
		List<BaseDict> fromType = baseDictService.queryBaseDictByDictTypeCode(CUSTOMER_FROM_TYPE);
		//所属行业
		List<BaseDict> industryType = baseDictService.queryBaseDictByDictTypeCode(CUSTOMER_INDUSTRY_TYPE);
		//客户级别
		List<BaseDict> levelType = baseDictService.queryBaseDictByDictTypeCode(CUSTOMER_LEVEL_TYPE);
		
		model.addAttribute("fromType", fromType);
		model.addAttribute("industryType", industryType);
		model.addAttribute("levelType", levelType);
		
		//分页查询数据
		Page<Customer> page = customerService.queryCustomerByQueryVo(vo);
		// 把分页查询的结果放到模型中
		model.addAttribute("page",page);
		//数据回显
		model.addAttribute("custName", vo.getCustName());
		model.addAttribute("custSource", vo.getCustSource());
		model.addAttribute("custIndustry", vo.getCustIndustry());
		model.addAttribute("custLevel", vo.getCustLevel());
		return "customer";
	}
	
	@RequestMapping(value = "edit.action", method = RequestMethod.GET)
	@ResponseBody
	public Customer editCustomer(Long id) {
		return customerService.queryCustomerById(id);
	}
	
	/**
	 * 根据id查询用户,返回更新后客户的json格式数据
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("update.action")
	@ResponseBody
	public String updateCustomerById(Customer customer) {
		customerService.updateCustomerById(customer);
		return "OK";
	}

	
	@RequestMapping("delete")
	@ResponseBody
	public String deleteCustomerById(Long id) {
		customerService.deleteCustomerById(id);
		return "Ok";
	}

}
